package com.kinghood.productcenter.controller.product;

import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.redis.RedisConstant;
import com.kinghood.infrastructure.model.dto.StatusRemarkReqDTO;
import com.kinghood.productcenter.common.model.dto.base.ListProductQueryDTO;
import com.kinghood.productcenter.common.model.dto.base.SkuQueryPageReqDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductReqDTO;
import com.kinghood.productcenter.common.model.dto.sku.GetProductSkuReqDTO;
import com.kinghood.productcenter.common.model.dto.sku.UpdateProductSkuReqDTO;
import com.kinghood.productcenter.common.model.vo.product.ProductDetailVO;
import com.kinghood.productcenter.manager.IProductSkuBaseManager;
import com.kinghood.productcenter.service.product.IProductSkuBaseService;
import com.kinghood.productcenter.service.product.handler.ProductBaseHandler;
import com.kinghood.redis.aspect.RedisLock;
import com.kinghood.redis.util.RedissonManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/productSku")
public class ProductSkuBaseController {

    private final IProductSkuBaseService productSkuBaseService;
    private final IProductSkuBaseManager productSkuBaseManager;
    private final RedissonManager redissonManager;


    /**
     * 根据搜索条件获取sku列表
     *
     * @param in 搜索条件
     * @return sku
     * @author zfj
     * @date 2024/1/17
     */
    @PostMapping("/queryPage")
    public KingHoodResponseVO queryPage(@Valid @RequestBody SkuQueryPageReqDTO in) {
        return KingHoodResponseVO.success(productSkuBaseService.queryPage(in));
    }

    /**
     * 导出sku
     *
     * @param param    参数
     * @param response 回答
     * @return
     * @author jp
     * @date 2024/01/23 16:46
     */
    @PostMapping("/exportSku")
    public void exportSku(@Valid @RequestBody SkuQueryPageReqDTO param, HttpServletResponse response) throws IOException {
        productSkuBaseManager.exportSku(param, response);
    }

    /**
     * sku详情
     *
     * @param in id
     * @return sku
     * @author zfj
     * @date 2024/1/17
     */
    @PostMapping("/getProductSku")
    public KingHoodResponseVO<ProductDetailVO> getProductSku(@RequestBody GetProductSkuReqDTO in) {

        return KingHoodResponseVO.success(productSkuBaseManager.getProductSku(in));
    }

    /**
     * excel导入
     *
     * @param file     文件
     * @param response 响应
     * @author zfj
     * @date 2023/12/25
     */
    @RedisLock(key = RedisConstant.LOCK_UPLOAD_PRODUCT_SKU)
    @PostMapping("/uploadProductSku")
    public void uploadProductSku(MultipartFile file, HttpServletResponse response) {

        log.info("uploadProductSku:===》导入商品SKU");
        productSkuBaseManager.uploadProductSku(file, response);

    }

    /**
     * 修改sku
     *
     * @param in sku信息
     * @return id
     * @author zfj
     * @date 2024/1/17
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-修改sku")
    @PostMapping("/addProductSku")
    public KingHoodResponseVO<Long> addProductSku(@Valid @RequestBody UpdateProductSkuReqDTO in) {

        ProductBaseHandler.handleSaveProduct(in.getSkuList(),in.getSkuPriceList());
        return KingHoodResponseVO.success(productSkuBaseManager.addProductSku(in));
    }


    /**
     * 修改sku
     *
     * @param in sku信息
     * @return id
     * @author zfj
     * @date 2024/1/17
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-修改sku")
    @PostMapping("/updateProductSku")
    public KingHoodResponseVO<Long> updateProductSku(@Valid @RequestBody UpdateProductSkuReqDTO in) {

        // 修改是针对单个sku
        in.setSku(in.getSkuList().get(0));
        in.getSku().getAttributes().putAll(in.getSkuPriceList().get(0).getAttributes());
        // 给附件和供应商skuCode
        if(!CollectionUtils.isEmpty(in.getSkuExtrasList())){
            in.getSkuExtrasList().forEach(x->x.setSkuCode(in.getSku().getSkuCode()));
        }
        if(!CollectionUtils.isEmpty(in.getSkuSupplierList())){
            in.getSkuSupplierList().forEach(x->x.setSkuCode(in.getSku().getSkuCode()));
        }
        return KingHoodResponseVO.success(productSkuBaseManager.updateProductSku(in));
    }

    /**
     * 获取sku属性
     *
     * @param businessCode 业务代码,定位页面的标识
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/23 15:25
     */
    @RequestMapping(value = "/getSkuFields")
    public KingHoodResponseVO getSkuFields(@RequestParam("businessCode") String businessCode) {
        return KingHoodResponseVO.success(productSkuBaseService.getSkuFields(businessCode));
    }

    /**
     * 获取sku搜索属性
     *
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/15 15:53
     */
    @RequestMapping(value = "/getSkuSearchFields")
    public KingHoodResponseVO getSkuSearchFields(@RequestParam("businessCode") String businessCode) {
        return KingHoodResponseVO.success(productSkuBaseService.getSkuSearchFields(businessCode));
    }

    /**
     * 更新sku状态
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/29
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SKU,content = "sku管理-修改状态")
    @PostMapping(value = "/updateSkuStatus")
    public KingHoodResponseVO updateProductStatus(@RequestBody StatusRemarkReqDTO param) {
        if (com.alibaba.nacos.common.utils.CollectionUtils.isEmpty(param.getIdList())) {
            throw new KingHoodException(-1, "id不能为空");
        }
        if (Objects.isNull(param.getStatus())) {
            throw new KingHoodException(-1, "状态不能为空");
        }
        productSkuBaseService.updateSkuStatus(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 删除sku
     *
     * @param id id
     * @author jp
     * @date 2023/12/29
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SKU,content = "sku管理-删除sku")
    @RequestMapping(value = "/delSku/{id}")
    public KingHoodResponseVO delSku(@PathVariable("id") Long id) {
        productSkuBaseService.delSku(id);
        return KingHoodResponseVO.success();
    }
}
